home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 2 / ETO Development Tools 2.iso / Tools - Objects / MacApp / MacApp CD Release / MacApp® 2.0.1 Tutorial / Chapter 07 / UIconEdit.inc1.p < prev    next >
Encoding:
Text File  |  1990-10-25  |  7.6 KB  |  274 lines  |  [TEXT/MPS ]

  1. {Copyright © 1989 by Apple Computer, Inc.  All rights reserved.}
  2.  
  3.  
  4.  
  5. CONST
  6.     kIconHBits =        32;                            { Number of horizontal bits in a bitmap.}
  7.     kIconVBits =        32;                            { Number of vertical bits in a bitmap.    }
  8.  
  9.     kIconSizeInBytes =    kIconHBits * kIconVBits DIV 8;    { Number of bytes in an bitmap.        }
  10.     kIconSizeInLongs =    kIconSizeInBytes DIV 4;        { Number of long words in a bitmap.        }
  11.     kMaxLong =            kIconSizeInLongs - 1;        { Max. addressable long word in bitmap.    }
  12.     
  13.     
  14.     { Resource identifiers }
  15.     
  16.     kSeedIconId =        1000;                        { Id of the seed icon resource.            }
  17.  
  18.  
  19.  
  20. TYPE
  21.     LongArrayHdl =        ^LongArrayPtr;
  22.     LongArrayPtr =        ^LongArray;
  23.     LongArray =            ARRAY [0..kMaxLong] OF LONGINT;
  24.  
  25.  
  26.  
  27. {-------------------------------------------------------------------------------------------}
  28. {--------------------------------TIconApplication methods-----------------------------------}
  29. {-------------------------------------------------------------------------------------------}
  30.  
  31. PROCEDURE TIconApplication.IIconApplication(iconFileType: OSType);
  32.  
  33. BEGIN
  34.     IApplication(iconFileType);
  35. END;
  36.  
  37.  
  38.  
  39. {-------------------------------------------------------------------------------------------}
  40.  
  41. FUNCTION  TIconApplication.DoMakeDocument(itsCmdNumber: CmdNumber): TDocument; OVERRIDE;
  42.  
  43. VAR
  44.     anIconDocument:        TIconDocument;
  45.  
  46. BEGIN
  47.     New(anIconDocument);                            { Create a TIconDocument object.        }
  48.     FailNIL(anIconDocument);                        { Make sure we were successful.            }
  49.     anIconDocument.IIconDocument;                    { Initialize it.                        }
  50.  
  51.     DoMakeDocument := anIconDocument;                { Return a reference to the document.    }
  52. END;
  53.     
  54.     
  55.     
  56.     
  57. {-------------------------------------------------------------------------------------------}
  58. {----------------------------------TIconDocument methods------------------------------------}
  59. {-------------------------------------------------------------------------------------------}
  60.  
  61. PROCEDURE TIconDocument.IIconDocument;
  62.  
  63. VAR anIconBitMap : TIconBitMap;
  64.  
  65. BEGIN
  66.     fIconBitMap := NIL;                                { Set this to NIL so that if IDocument    }
  67.                                                     { fails, TIconDocument.Free works okay.    }
  68.  
  69.     IDocument(kFileType,                             { This document's file type.            }
  70.               kSignature,                             { This document's creator.                }
  71.               kUsesDataFork,                         { This document does use the data fork    }
  72.               NOT kUsesRsrcFork,                    { …but doesn't use the resource fork.    }
  73.               NOT kDataOpen,                        { We don't want the data fork kept open    }
  74.               NOT kRsrcOpen);                        { …nor the resource fork.                }
  75.  
  76.     New(anIconBitMap);                                { Allocate a new icon bitmap.            }
  77.     FailNil(anIconBitMap);                            { Fail if we can't allocate the handle.    }
  78.     anIconBitMap.IIconBitMap;                        { Initialize it.                        }
  79.     
  80.     fIconBitMap := anIconBitMap;                    { Store a reference to it in a field.     }
  81. END;
  82.  
  83.  
  84.  
  85. {-------------------------------------------------------------------------------------------}
  86.  
  87. PROCEDURE TIconDocument.DoInitialState; OVERRIDE;
  88.     { This method is called to set the document's data to the "new" state, as when the user    }
  89.     { chooses to open a new document instead of an existing one.  We set the value of the     }
  90.     { document's icon bit map to that of a "seed" icon in our resource file.  That way we     }
  91.     { can the document's initial state simply by changing the "seed" icon resource.            }
  92.  
  93. VAR
  94.     seedIcon:        Handle;
  95.  
  96. BEGIN
  97.     seedIcon := GetIcon(kSeedIconId);                { Get the data from a resource.            }
  98.     FailNilResource(seedIcon);                      { Check for resource failure.           }
  99.     fIconBitMap.SetIconBitMap(seedIcon);            { And set the icon bit map to it.        }
  100. END;
  101.  
  102.  
  103.  
  104. {-------------------------------------------------------------------------------------------}
  105.  
  106. PROCEDURE TIconDocument.Free; OVERRIDE;
  107.     
  108. BEGIN
  109.     FreeIfObject(fIconBitMap);                        { Dispose of the icon object if non-Nil.}
  110.  
  111.     INHERITED Free;
  112. END;
  113.  
  114.  
  115.  
  116. {-------------------------------------------------------------------------------------------}
  117.  
  118. PROCEDURE TIconDocument.DoMakeViews(forPrinting: boolean); OVERRIDE;
  119.  
  120. BEGIN
  121.     { Do nothing for now. }
  122. END;
  123.  
  124.         
  125.  
  126.     
  127. {-------------------------------------------------------------------------------------------}
  128. {----------------------------------TIconBitMap methods--------------------------------------}
  129. {-------------------------------------------------------------------------------------------}
  130.  
  131. PROCEDURE TIconBitMap.IIconBitMap;
  132.  
  133. BEGIN
  134.     fDataHandle := NewPermHandle(kIconSizeInBytes);    { Allocate a handle for the bitmap.        }
  135.     FailNil(fDataHandle);                            { Fail if we can't allocate the handle.    }
  136. END;
  137.  
  138.  
  139.  
  140. {-------------------------------------------------------------------------------------------}
  141.  
  142. PROCEDURE TIconBitMap.Free; OVERRIDE;
  143.  
  144. BEGIN
  145.     DisposIfHandle(fDataHandle);                    { dispose of icon data.                    }
  146. END;
  147.  
  148.  
  149.  
  150. {-------------------------------------------------------------------------------------------}
  151.  
  152. PROCEDURE TIconBitMap.SetIconBitMap(theBitMap : Handle);
  153.  
  154. BEGIN
  155.     BlockMove(theBitMap^, fDataHandle^,                { …then copy it into the document's     }
  156.                 kIconSizeInBytes)                    { …icon bitmap.                            }
  157. END;
  158.             
  159.             
  160.  
  161. {-------------------------------------------------------------------------------------------}
  162.  
  163. PROCEDURE TIconBitMap.Clear;
  164.  
  165. VAR
  166.     iconAsLongArray:    LongArrayHdl;
  167.     i:                    INTEGER;
  168.  
  169. BEGIN
  170.     iconAsLongArray := LongArrayHdl(fDataHandle);    { Cast to array of longints.            }
  171.  
  172.     FOR i := 0 TO kMaxLong DO                        { Clear the bits 32 at a time.            }
  173.         iconAsLongArray^^[i] := 0;
  174. END;
  175.  
  176.  
  177.  
  178. {-------------------------------------------------------------------------------------------}
  179.  
  180. PROCEDURE TIconBitMap.Invert;
  181.  
  182. VAR
  183.     iconAsLongArray:    LongArrayHdl;
  184.     i:                    INTEGER;
  185.  
  186. BEGIN
  187.     iconAsLongArray := LongArrayHdl(fDataHandle);    { Cast to array of longints.            }
  188.  
  189.     FOR i := 0 TO kMaxLong DO                        { Invert the bits 32 at a time.            }
  190.         iconAsLongArray^^[i] := BNOT(iconAsLongArray^^[i]);
  191. END;
  192.  
  193.  
  194.  
  195. {-------------------------------------------------------------------------------------------}
  196.  
  197. PROCEDURE TIconBitMap.IconBitToWordBit (iconBit: Point; VAR word, bit: INTEGER);
  198.     { This converts the given icon bit to a word and bit in an array of long words. }
  199.  
  200. VAR
  201.     bitNumber:        INTEGER;
  202.  
  203. BEGIN
  204.     bitNumber := iconBit.v * kIconVBits + iconBit.h;
  205.     word := bitNumber DIV 32;
  206.     bit := 31 - (bitNumber MOD 32);
  207. END;
  208.  
  209.  
  210.  
  211. {-------------------------------------------------------------------------------------------}
  212.  
  213. FUNCTION  TIconBitMap.GetBit (iconBit: Point): BOOLEAN;
  214.     { Returns the state of the given bit in the icon being drawn. }
  215.  
  216. VAR
  217.     word:            INTEGER;
  218.     bitInWord:        INTEGER;
  219.  
  220. BEGIN
  221.     IconBitToWordBit(iconBit, word, bitInWord);
  222.  
  223.     GetBit := BTst(LongArrayHdl(fDataHandle)^^[word], bitInWord);
  224. END;
  225.  
  226.  
  227.  
  228. {-------------------------------------------------------------------------------------------}
  229.  
  230. PROCEDURE TIconBitMap.SetBit (iconBit: Point; turnBitOn: BOOLEAN);
  231.     { Set the state of the given bit in the icon being drawn. }
  232.  
  233. VAR
  234.     word:            INTEGER;
  235.     bitInWord:        INTEGER;
  236.  
  237. BEGIN
  238.     IconBitToWordBit(iconBit, word, bitInWord);
  239.  
  240.     {$H-}                                            { So the compiler thinks this is unsafe.}
  241.     IF turnBitOn THEN
  242.         BSet(LongArrayHdl(fDataHandle)^^[word], bitInWord)
  243.     ELSE
  244.         BClr(LongArrayHdl(fDataHandle)^^[word], bitInWord);
  245.     {$H+}
  246. END;
  247.  
  248.  
  249.  
  250. {-------------------------------------------------------------------------------------------}
  251.  
  252. FUNCTION TIconBitMap.Copy: TIconBitMap;
  253.  
  254. VAR
  255.     copyOfIcon:        TIconBitMap;
  256.  
  257. BEGIN
  258.     New(copyOfIcon);                                { Create a TIcon object.                }
  259.     FailNIL(copyOfIcon);                            { Make sure we were successful.            }
  260.     copyOfIcon.IIconBitMap;                            { Initialize it.                        }
  261.     copyOfIcon.SetIconBitMap(fDataHandle);            { Copy the data.                        }
  262.     Copy := copyOfIcon;                                { Return a reference to the new handle.    }
  263. END;
  264.  
  265.  
  266.             
  267. {-------------------------------------------------------------------------------------------}
  268.  
  269. PROCEDURE TIconBitMap.CopyDataTo (anIcon: TIconBitMap);
  270.  
  271. BEGIN
  272.     anIcon.SetIconBitMap(fDataHandle);                { Copy data to the new icon.            }
  273. END;
  274.